<?php

namespace common\behavior\CommonMessagePushing;


use common\service\BehaviorService;
use common\service\ReviewRequestMessageService;
use common\service\UserService;
use Webman\Stomp\Client;

/**
 * 延期单确认结果 消息通知
 */
class DelayOrderResultBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'review_names' => '',
                'delay_order' => '',
                'is_pass' => 'intval',
                'reviewed' => 'intval',
            ],
            [
                'review_names' => [],
                'delay_order' => [],
                'is_pass' => 0,
                'reviewed' => 0,
            ],
            'data',
            ['type' => 'delay_order_result'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($reviewNames, $delayOrder, $isPass, $reviewed) = $params;
        if (($isPass === 0) || empty($reviewNames) || empty($reviewed) || empty($delayOrder)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $firstName = $isPass ? '延期单申请通过' : '有任务申请延期不通过';
        $messageParam = [
            'first' => $firstName,
            'delay_order_name' => $delayOrder['name'],
            'reviewer_name' => $reviewNames,
            'review_at' => date("Y/m/d H:i", $reviewed),
            'remark' => '请及时查看',
            'delay_order_id' => $delayOrder['id']
        ];
        $messageTemplateId = ReviewRequestMessageService::getInstance()->delayOrderResultMessageTemplateId();
        $creator = UserService::getInstance()->getUserFindField(['id' => $delayOrder['created_by']], '*');

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $messageParam,
                'receiver' => $creator['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'is_popup' => 1,
                'message_key' => 'task_delay_order_review_result',
            ]],
        ]));
        /***********  结束  ************/
    }
}
